package routers

import (
	"beego/controllers"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

func init() {
	// 过滤中间件,验证用户是否已经登录，应用于 `/article` 开头的所有请求
	var FilterUser = func(ctx *context.Context) {
		_, ok := ctx.Input.Session("username").(string)
		if !ok && ctx.Request.RequestURI != "/login" {
			ctx.Redirect(302, "/login")
		}
	}

	beego.InsertFilter("/article/*", beego.BeforeRouter, FilterUser)

	beego.Router("/", &controllers.MainController{})
	beego.Router("/user", &controllers.UserController{})
	beego.Router("/register", &controllers.UserController{}, "get:Register;post:HandleRegister")
	// 自定义控制器,get,post方法
	beego.Router("/login", &controllers.UserController{}, "get:Login;post:HandleLogin")
	beego.Router("/logout", &controllers.UserController{}, "get:Logout")
	beego.Router("/article", &controllers.ArticleController{}, "get:Index")
	beego.Router("/article/detail", &controllers.ArticleController{}, "get:Detail")
	beego.Router("/article/add", &controllers.ArticleController{}, "get:ArticleAdd;post:HandleArticleAdd")
	beego.Router("/article/update", &controllers.ArticleController{}, "get:Update;post:HandleUpdate")
	beego.Router("/article/delete", &controllers.ArticleController{}, "get:Delete")
	// Tag的操作
	beego.Router("/article/tag", &controllers.ArticleController{}, "get:TagAdd;post:HandleTagAdd")
	beego.Router("/article/tag/update", &controllers.ArticleController{}, "get:TagUpdate;post:HandleTagUpdate")
	beego.Router("/article/tag/delete", &controllers.ArticleController{}, "get:TagDelete")
	// 执行命令的接口
	beego.Router("/api/duration", &controllers.APIController{}, "get:Duration")
}
